package com.lb.jwitter.action;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import java.util.Collection;
import com.lb.jwitter.util.ApplicationContextProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.lb.jwitter.service.ConditionReport;
import com.lb.jwitter.util.Message;
import com.opensymphony.xwork2.Action;
import com.lb.jwitter.service.TblCTDTKhoiNganh;
import com.lb.jwitter.service.TblCTDTKhoiNganhService;
import com.lb.jwitter.service.TblQuyenChiTietService;
import com.lb.jwitter.service.TblTaiKhoan;
import com.lb.jwitter.service.TblCTDTThayTheMonHoc;
import com.lb.jwitter.service.TblCTDTThayTheMonHocService;
import com.lb.jwitter.service.TblCTDTMonHoc;
import com.lb.jwitter.service.TblCTDTMonHocService;

import com.lb.jwitter.service.TblCTDTMonHoc;
import com.lb.jwitter.service.TblCTDTMonHocService;

import com.lb.jwitter.service.TblCTDTMonHoc;
import com.lb.jwitter.service.TblCTDTMonHocService;

import com.lb.jwitter.service.TblCTDTMonHoc;
import com.lb.jwitter.service.TblCTDTMonHocService;

/**
 * @author QUOCVIET
 * 
 */

@ContextConfiguration(locations = "classpath:beans.xml")
public class TblCTDTThayTheMonHocUpdateAction {
	private int pageSize;

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	private int page;

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	private int pages;

	public int getPages() {
		return pages;
	}

	public void setPages(int pages) {
		this.pages = pages;
	}

	private String dataString;

	public String getDataString() {
		return dataString;
	}

	private Message msg;

	public Message getMsg() {
		return msg;
	}

	private int id;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	private TblCTDTMonHocService tblCTDTMonHocService;

	private Long monHoc1Id;

	public void setMonHoc1Id(Long monHoc1Id) {
		this.monHoc1Id = monHoc1Id;
	}

	public Long getMonHoc1Id() {
		return monHoc1Id;
	}

	private Long monHoc2Id;

	public void setMonHoc2Id(Long monHoc2Id) {
		this.monHoc2Id = monHoc2Id;
	}

	public Long getMonHoc2Id() {
		return monHoc2Id;
	}

	private Long mHThayThe1Id;

	public void setMHThayThe1Id(Long mHThayThe1Id) {
		this.mHThayThe1Id = mHThayThe1Id;
	}

	public Long getMHThayThe1Id() {
		return mHThayThe1Id;
	}

	private Long mHThayThe2Id;

	public void setMHThayThe2Id(Long mHThayThe2Id) {
		this.mHThayThe2Id = mHThayThe2Id;
	}

	public Long getMHThayThe2Id() {
		return mHThayThe2Id;
	}

	// ----------------chumano------------------
	private String maMonHoc1;

	public void setMaMonHoc1(String monhoc) {
		this.maMonHoc1 = monhoc;
	}

	public String getMaMonHoc1() {
		return maMonHoc1;
	}

	private String maMonHoc2;

	public void setMaMonHoc2(String monhoc) {
		this.maMonHoc2 = monhoc;
	}

	public String getMaMonHoc2() {
		return maMonHoc2;
	}

	private String maMHThayThe1;

	public void setMaMHThayThe1(String monhoc) {
		this.maMHThayThe1 = monhoc;
	}

	public String getMaMHThayThe1() {
		return maMHThayThe1;
	}

	private String maMHThayThe2;

	public void setMaMHThayThe2(String monhoc) {
		this.maMHThayThe2 = monhoc;
	}

	public String getMaMHThayThe2() {
		return maMHThayThe2;
	}
	
	private String maKhoiNganh;

	public void setMaKhoiNganh(String  maKhoiNganh) {
		this. maKhoiNganh =  maKhoiNganh;
	}

	public String getMaKhoiNganh() {
		return  maKhoiNganh;
	}
	private TblCTDTKhoiNganhService tblCTDTKhoiNganhService;
	// ==============================================

	private int khoaHoc;

	public void setKhoaHoc(int khoaHoc) {
		this.khoaHoc = khoaHoc;
	}

	public int getKhoaHoc() {
		return khoaHoc;
	}

	private TblCTDTThayTheMonHoc tblCTDTThayTheMonHoc;

	public void setTblCTDTThayTheMonHoc(
			TblCTDTThayTheMonHoc tblCTDTThayTheMonHoc) {
		this.tblCTDTThayTheMonHoc = tblCTDTThayTheMonHoc;
	}

	private TblCTDTThayTheMonHocService tblCTDTThayTheMonHocService;

	private int preUpdate(Message msg, TblCTDTThayTheMonHoc obj) {
		msg.setPreMessage_b(true);
		msg.setPreMessage_s("success");
		return 1;
	}

	private int postUpdate(Message msg, TblCTDTThayTheMonHoc obj) {
		msg.setPostMessage_b(true);
		msg.setPostMessage_s("success");
		return 1;
	}

	private TblTaiKhoan taiKhoanDangNhap;

	public TblTaiKhoan getTaiKhoanDangNhap() {
		return taiKhoanDangNhap;
	}

	public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) {
		this.taiKhoanDangNhap = taiKhoanDangNhap;
	}

	private TblQuyenChiTietService tblQuyenChiTietService;
	@Autowired
	// Injected by Spring
	ApplicationContextProvider contextProvider;

	public String execute() throws Exception {
		try {
			Map session = ActionContext.getContext().getSession();
			taiKhoanDangNhap = (TblTaiKhoan) session.get("taiKhoan");
			if (taiKhoanDangNhap == null)
				return "noprivilege";
			ApplicationContext context = ApplicationContextProvider
					.getApplicationContext();
			tblQuyenChiTietService = (TblQuyenChiTietService) context
					.getBean("tbl_Quyen_ChiTietClient");
			if (!tblQuyenChiTietService.checkRight(taiKhoanDangNhap
					.getMaNhomQuyen().getId(), "tbl_CTDT_ThayTheMonHoc", "sua")) {
				return "noprivilege";
			}
			tblCTDTThayTheMonHocService = (TblCTDTThayTheMonHocService) context
					.getBean("tbl_CTDT_ThayTheMonHocClient");

			tblCTDTMonHocService = (TblCTDTMonHocService) context
					.getBean("tbl_CTDT_MonHocClient");

			msg = new Message();

			// -----------------------------------------
			// Tim kiem mon hoc voi ma mon hoc
			List<TblCTDTMonHoc> monhocs = findMonHocWithMa(maMonHoc1,
					tblCTDTMonHocService);
			if (monhocs != null && monhocs.size() > 0) {
				monHoc1Id = monhocs.get(0).getId();
			} else {
				return Action.ERROR;
			}
			//
			monhocs = findMonHocWithMa(maMonHoc2, tblCTDTMonHocService);
			if (monhocs != null && monhocs.size() > 0) {
				monHoc2Id = monhocs.get(0).getId();
			} else {
				return Action.ERROR;
			}
			//
			monhocs = findMonHocWithMa(maMHThayThe1, tblCTDTMonHocService);
			if (monhocs != null && monhocs.size() > 0) {
				mHThayThe1Id = monhocs.get(0).getId();
			} else {
				return Action.ERROR;
			}
			//
			monhocs = findMonHocWithMa(maMHThayThe2, tblCTDTMonHocService);
			if (monhocs != null && monhocs.size() > 0) {
				mHThayThe2Id = monhocs.get(0).getId();
			} else {
				return Action.ERROR;
			}
			
			//tim khoi nganh
			List<ConditionReport> cons = new ArrayList<ConditionReport>();
			{
				ConditionReport con = new ConditionReport();
				con.setCol("ma");
				con.setValue1(maKhoiNganh);
				con.setOperator("eq");
				cons.add(con);
			}

			List<TblCTDTKhoiNganh> khoiNganh = tblCTDTKhoiNganhService.findtblCTDTKhoiNganhByCondition(cons, 0, 0);
			if (khoiNganh == null || khoiNganh.size() == 0) {
				return Action.ERROR;
			}
			// ==========================================

			tblCTDTThayTheMonHoc = tblCTDTThayTheMonHocService.gettblCTDTThayTheMonHoc(new Long(id));
			tblCTDTThayTheMonHoc.setMonHoc1Id(tblCTDTMonHocService.gettblCTDTMonHoc(new Long(monHoc1Id)));
			tblCTDTThayTheMonHoc.setMonHoc2Id(tblCTDTMonHocService.gettblCTDTMonHoc(new Long(monHoc2Id)));
			tblCTDTThayTheMonHoc.setMHThayThe1Id(tblCTDTMonHocService.gettblCTDTMonHoc(new Long(mHThayThe1Id)));
			tblCTDTThayTheMonHoc.setMHThayThe2Id(tblCTDTMonHocService.gettblCTDTMonHoc(new Long(mHThayThe2Id)));
			tblCTDTThayTheMonHoc.setKhoiNganhId(tblCTDTKhoiNganhService.gettblCTDTKhoiNganh(khoiNganh.get(0).getId()));
			
			
			preUpdate(msg, tblCTDTThayTheMonHoc);
			if (msg.isPreMessage_b()) {
				tblCTDTThayTheMonHocService
						.updatetblCTDTThayTheMonHoc(tblCTDTThayTheMonHoc);
				postUpdate(msg, tblCTDTThayTheMonHoc);
				if (msg.isPostMessage_b() == false) {
					return Action.ERROR;
				}
			} else {
				return Action.ERROR;
			}

			return Action.SUCCESS;
		} catch (Exception e) {
			System.out.println("Loi Update tbl_CTDT_ThayTheMonHoc Action");
			e.printStackTrace();
			return Action.ERROR;
		}
	}

	private List<TblCTDTMonHoc> findMonHocWithMa(String ma,
			TblCTDTMonHocService tblCTDTMonHocService) {
		List<ConditionReport> cons = new ArrayList<ConditionReport>();
		{
			ConditionReport con = new ConditionReport();
			con.setCol("ma");
			con.setValue1(ma);
			con.setOperator("eq");
			cons.add(con);
		}

		List<TblCTDTMonHoc> monhocs = tblCTDTMonHocService
				.findtblCTDTMonHocByCondition(cons, 0, 0);

		return monhocs;
	}
}
